package com.cbarmstrong.FinanceWeb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

import com.cbarmstrong.Finance.Data;
import com.cbarmstrong.Finance.Ticker;
import com.cbarmstrong.Finance.TradeSet;
import com.cbarmstrong.MarketWatcher.IndicatorChromosome;

public class PopulationDistribution {

	Connection c;
	Statement s;
	ResultSet r;
	Ticker t = null;
	double bear=0, bull=0, out=0, count=0;

	public double[] getHistoricDistribution(int id, int offset){
		double[] ret={0,0,0,0};
		try {
			c = Data.initConnection();
			s = c.createStatement();
			r= s.executeQuery("SELECT name FROM stock WHERE id="+id);
			Calendar cal=Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, offset*-1);
			r=s.executeQuery("SELECT buy, hold, sell, position FROM ratios WHERE date='"+new java.sql.Date(cal.getTime().getTime())+"' AND stockid="+id);
			r.first();
			ret[0]=r.getDouble("buy");
			ret[1]=r.getDouble("hold");
			ret[2]=r.getDouble("sell");
			ret[3]=r.getDouble("position");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return ret;
	}
	
	public double[] getDistribution(int id){
		double price=0, position=0;
		bull=0;
		bear=0;
		out=0;
		try {
			c = Data.initConnection();
			s = c.createStatement();
			r= s.executeQuery("SELECT name FROM stock WHERE id="+id);
			while(r.next()){
				t=new Ticker(r.getString("name"), new Integer(System.getProperty("tickerDays","1000")));
			}	
			r = s.executeQuery("SELECT DISTINCT indicators FROM populations WHERE stockid="+id);
			IndicatorChromosome cop;
			TradeSet ts;
			while(r.next()){
				cop=new IndicatorChromosome();
				cop.initiate(t);
				for(String ind : r.getString(1).split(":")){
					String[] inds=ind.split(" ");
					if(ind.split(" ").length>0){
						if(inds[0].compareTo("cross")==0) cop.addCrossIndicator(inds[1], inds[2]);
						if(inds[0].compareTo("range")==0) cop.addRangeIndicator(inds[1], inds[2], inds[3], new Integer(inds[4]));
					}
				}
				ts=cop.execute();
				if(ts.currentlyLong()){ bull+=cop.getFitness(); }
				if(ts.currentlyShort()){ bear+=cop.getFitness(); }
				if(ts.currentlyOut()){ out+=cop.getFitness(); }
			}
			count=bear+bull+out;
			bear=bear*100/count;
			bull=bull*100/count;
			out=out*100/count;
			Calendar cal = Calendar.getInstance();
			position=getHistoricDistribution(id,1)[3];
			if(position==1){ if(bull<50){ position=0; } }
			if(position==-1){ if(bear<50){ position=0; } }
			if(position==0){ 
				if(bull>80){ position=1; }
				if(bear>80){ position=-1; }
			}
			price=t.getCloses().lastElement()+(position*t.getSpread());
			if(s.executeUpdate("UPDATE ratios set buy="+bull+", sell="+bear+", hold="+out+", price="+price+", position="+position+" WHERE date='"+new java.sql.Date(cal.getTime().getTime())+"' AND stockid="+id)==0){
				s.executeUpdate("INSERT INTO ratios VALUES ( "+id+" , '" + new java.sql.Date(cal.getTime().getTime()) + "' , "+ bull +" , "+ out +" , "+ bear +" , "+ position +" , "+ price +" )");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		double[] ret={bull, out, bear, position, price};
		return ret;
	}
	
}
